package com.draven.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.entity.NoteTag;
import com.draven.system.mapper.NoteTagMapper;
import com.draven.system.service.INoteTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 笔记标签Service业务层处理
 * 
 * @author draven
 */
@Service
public class NoteTagServiceImpl extends ServiceImpl<NoteTagMapper, NoteTag> implements INoteTagService {

    @Autowired
    private NoteTagMapper noteTagMapper;

    /**
     * 查询标签列表
     * 
     * @param tag 标签信息
     * @return 标签列表
     */
    @Override
    public List<NoteTag> selectTagList(NoteTag tag) {
        LambdaQueryWrapper<NoteTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(tag.getTagName()), NoteTag::getTagName, tag.getTagName());
        queryWrapper.eq(tag.getStatus() != null, NoteTag::getStatus, tag.getStatus());
        queryWrapper.orderByDesc(NoteTag::getUseCount);
        queryWrapper.orderByDesc(NoteTag::getCreateTime);
        return noteTagMapper.selectList(queryWrapper);
    }

    /**
     * 查询标签列表（包含使用次数）
     * 
     * @return 标签列表
     */
    @Cacheable(cacheNames = "noteTag", key = "'selectTagListWithCount'")
    @Override
    public List<NoteTag> selectTagListWithCount() {
        return noteTagMapper.selectTagListWithCount();
    }

    /**
     * 根据笔记ID查询标签列表
     * 
     * @param noteId 笔记ID
     * @return 标签列表
     */
    @Override
    public List<NoteTag> selectTagsByNoteId(Long noteId) {
        return noteTagMapper.selectTagsByNoteId(noteId);
    }

    /**
     * 查询热门标签
     * 
     * @param limit 限制数量
     * @return 标签列表
     */
    @Cacheable(cacheNames = "noteTag", key = "'selectHotTags:' + #limit")
    @Override
    public List<NoteTag> selectHotTags(Integer limit) {
        return noteTagMapper.selectHotTags(limit);
    }

    /**
     * 根据标签ID查询标签信息
     * 
     * @param tagId 标签ID
     * @return 标签信息
     */
    @Override
    public NoteTag selectTagById(Long tagId) {
        return noteTagMapper.selectById(tagId);
    }

    /**
     * 校验标签名称是否唯一
     * 
     * @param tag 标签信息
     * @return 结果
     */
    @Override
    public boolean checkTagNameUnique(NoteTag tag) {
        Long tagId = tag.getId() == null ? -1L : tag.getId();
        NoteTag info = noteTagMapper.selectByTagName(tag.getTagName());
        if (info != null && info.getId().longValue() != tagId.longValue()) {
            return false;
        }
        return true;
    }

    /**
     * 新增标签
     * 
     * @param tag 标签信息
     * @return 结果
     */
    @CacheEvict(cacheNames = "noteTag", allEntries = true)
    @Override
    public int insertTag(NoteTag tag) {
        return noteTagMapper.insert(tag);
    }

    /**
     * 修改标签
     * 
     * @param tag 标签信息
     * @return 结果
     */
    @CacheEvict(cacheNames = "noteTag", allEntries = true)
    @Override
    public int updateTag(NoteTag tag) {
        return noteTagMapper.updateById(tag);
    }

    /**
     * 批量删除标签
     * 
     * @param tagIds 需要删除的标签ID
     * @return 结果
     */
    @CacheEvict(cacheNames = "noteTag", allEntries = true)
    @Override
    public int deleteTagByIds(Long[] tagIds) {
        return noteTagMapper.deleteBatchIds(Arrays.asList(tagIds));
    }

    /**
     * 删除标签信息
     * 
     * @param tagId 标签ID
     * @return 结果
     */
    @CacheEvict(cacheNames = "noteTag", allEntries = true)
    @Override
    public int deleteTagById(Long tagId) {
        return noteTagMapper.deleteById(tagId);
    }
}